<?php
namespace Cashio\Controller;
use Common\Controller\AdminbaseController;

class CashexadminController extends AdminbaseController{
	function _initialize() {
		parent::_initialize();
						
	}
	function index(){				
		$this->display();     // 输出模板		            
	}
	function cashexport(){
		//$lists = $this->smanage_model->order(array("id"=>"asc"))->select();
		//dump($lists);
		//$this->assign('lists',$lists);
		//dump($_SESSION);
		//dump($_SESSION['user']['user_nicename']);
		$admin_name = $_SESSION['name'];//取当前用户的session名字；
		//dump($admin_name);
		$admin_info = D("Users")->where(array("user_login"=>$admin_name))->find();//根据名字从usernas表拿到当前用户信息；
		$admin_nicename = $admin_info['user_nicename'];	//取到该用户的昵称；
		//dump($admin_nicename);	
		$cashio_model = D("Cashio"); //分页使用由此开始，必须先实例化model，可以不用实例Page类。		
		$count = $cashio_model->where(array("cangku"=>1))->count();		
		$Page  = $this->Page($count,15);  // 实例化分页类 传入总记录数和每页显示的记录数(15)
		$show  = $Page->show("Admin");   // 分页显示输出
		// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
		$lists  = $cashio_model->order('id desc')->limit($Page->firstRow . ',' . $Page->listRows)->where(array('action_bz'=>'出库'))->select();//当用户是超级管理员时，显示所有设备信息
		$lists_count= $cashio_model->order('id desc')->limit($Page->firstRow . ',' . $Page->listRows)->where(array('action_bz'=>'出库'))->count();//总记录数
		
		//dump($lists);			
		$sbtype = M('Sbtype');
		$gtype = $sbtype->select();	
		$sbjg = M('Sbjg');
		$gjg = $sbjg->select();			
		$this -> assign('gjg',$gjg);
		$this -> assign('lists_count',$lists_count); //输出总记录数		
		$this -> assign('gtype',$gtype);	
		$this->assign('lists',$lists);  // 赋值数据集
		$this->assign('Page',$show);   // 赋值分页输出				
		$this->display();     // 输出模板		            
	}	

	function diaobo(){							
		$mingcheng = I("get.mingcheng");
		$sbtype = M('Cashtype');
		$gtype = $sbtype->where(array('cash_name'=>$mingcheng))->find();						
		$this -> assign('gtype',$gtype);
			$cashio_model = D('Cashio');
			$map['mingcheng'] = $mingcheng;
			$map['cangku'] = 1;
			$q = $cashio_model->field('mingcheng,sum(shuliang) as shuliang,danwei')->order('id desc')->where($map)->find();			
	    $this -> assign('qqq',$q);						
		$this->display();					
	}
	function diaobo_post(){
			$cashio_model = D('Cashio');			
			if(IS_POST){
				if(!I('post.shuliang')){			
				$this->error("调拨失败，调拨数不能为空");
			}					
				if(I('post.shuliang')>I('post.dangqiankucun')){			
				$this->error("调拨失败，调拨数不能大于库存量");
			}	
				if(I('post.danwei')=='把'){
					$dataList[] = array(
						'mingcheng'=>I('post.mingcheng'),
						'mianzhi'=>I('post.mianzhi'),
						'shuliang'=>-I('post.shuliang'),
						'danwei'=>I('post.danwei'),
						'jine'=>-I('post.shuliang')*I('post.mianzhi')*100,
						'action_bz'=>'出库',
						'cangku'=>1,
						'create_time'=>date("Y-m-d H:i:s"),					
						);
					$dataList[] = array(
						'mingcheng'=>I('post.mingcheng'),
						'mianzhi'=>I('post.mianzhi'),
						'shuliang'=>I('post.shuliang'),
						'danwei'=>I('post.danwei'),
						'jine'=>I('post.shuliang')*I('post.mianzhi')*100,
						'action_bz'=>'入库',
						'cangku'=>2,
						'create_time'=>date("Y-m-d H:i:s"),			
						);
				}elseif(I('post.danwei')=='张'){
					$dataList[] = array(
						'mingcheng'=>I('post.mingcheng'),
						'mianzhi'=>I('post.mianzhi'),
						'shuliang'=>-I('post.shuliang'),
						'danwei'=>I('post.danwei'),
						'jine'=>-I('post.shuliang')*I('post.mianzhi'),
						'action_bz'=>'出库',
						'cangku'=>1,
						'create_time'=>date("Y-m-d H:i:s"),					
						);
					$dataList[] = array(
						'mingcheng'=>I('post.mingcheng'),
						'mianzhi'=>I('post.mianzhi'),
						'shuliang'=>I('post.shuliang'),
						'danwei'=>I('post.danwei'),
						'jine'=>I('post.shuliang')*I('post.mianzhi'),
						'action_bz'=>'入库',
						'cangku'=>2,
						'create_time'=>date("Y-m-d H:i:s"),			
						);
				}

				if(!$cashio_model->create($dataList)){
					$this -> error($cashio_model->geterror());//添加有自动验证规则时，应在此处进行错误提示和判断，不能在最后$result处写，否则报不出来错误！					
				}else{
					$result = $cashio_model->addAll($dataList);					
					if($result!==false){
						$this->success("调拨成功！",U('Cashimadmin/kucun'));
					}else{
						$this->error("调拨失败！");
					}					
				}
			}
		}

	function ssearch(){
		$typename = I("post.type");
		$jg = I("post.jg");
		$keyword = I("post.keyword");
		//dump($keyword);
		//dump($typename);
		$smanage_model = D("Smanage");
		if($typename){
			$ssearchlists = $smanage_model->where(array("sblb"=>$typename))->select();		
			//dump($ssearchlists);
			$ssearchcount = $smanage_model->where(array("sblb"=>$typename))->count();
			//dump($ssearchcount);
		}
		if($jg){
			$ssearchlists = $smanage_model->where(array("sydw"=>$jg))->select();
			$ssearchcount = $smanage_model->where(array("sydw"=>$jg))->count();	
		}
		if($keyword){
			//dump($keyword);						
			$ssearchlists = $smanage_model->where(array("sbsn"=>$keyword))->select();
			$ssearchcount = $smanage_model->where(array("sbsn"=>$keyword))->count();			
		}		
		$this -> assign('ssearchlists',$ssearchlists);
		$this -> assign('ssearchcount',$ssearchcount);		
		$this -> display();		
	}
	
	function add(){		
		//$admin_name = $_SESSION['name'];//取当前用户的session名字；
		//dump($admin_name);
		//$admin_info = D("Users")->where(array("user_login"=>$admin_name))->find();//根据名字从usernas表拿到当前用户信息；
		//dump($admin_info);
		//$this -> assign('admin_info',$admin_info);		 
		$sbtype = M('Cashtype');
		$gtype = $sbtype->select();
		//$sbjg = M('Sbjg');
		//$gjg = $sbjg->select();				
		$this -> assign('gtype',$gtype);
		//$this -> assign('gjg',$gjg);			
		$this->display();						
	}
	function add_post(){
			$cashio_model = D('Cashio');			
			if(IS_POST){				
				$data=I("post.");				
				$map['mingcheng'] = $_POST['mingcheng'];
				$map['cangku'] = 2;
				$p = $cashio_model->field('sum(shuliang) as shuliang')->order('id desc')->where($map)->find();	

				if($_POST['shuliang']<=$p['shuliang'] & $_POST['shuliang']>0){
				if($_POST['danwei']=='把'){
					$data['jine'] = -$_POST['shuliang']*$_POST['mianzhi']*100;
				}else{
					$data['jine'] = -$_POST['shuliang']*$_POST['mianzhi'];
				}
				$data['shuliang'] = -$_POST['shuliang'];
				$data['action_bz'] = '出库';
			}else{
				$this->error("出库失败，出库数不能为空且不能大于该币值的库存量");
			}
				if(!$cashio_model->create($data)){
					$this -> error($cashio_model->geterror());//添加有自动验证规则时，应在此处进行错误提示和判断，不能在最后$result处写，否则报不出来错误！					
				}else{
					$result = $cashio_model->add();					
					if($result!==false){
						$this->success("出库成功！",U('Cashexadmin/cashexport'));
					}else{
						$this->error("添加失败！");
					}					
				}
			}
		}	
	
		
	function delete(){
		if(isset($_GET['id'])){
		$id = intval(I('get.id'));
		$result = D("Cashio")->where(array("id"=>$id))->delete();
		if($result!==false){
			$this->success("删除成功！", U("Cashimadmin/kucun"));
			}else{
			$this->error('删除失败！');
		}
		}
		if(isset($_POST['ids'])){
		$ids=join(",",$_POST['ids']);
		$result = D("Cashio")->where("id in ($ids)")->delete();			
		if ($result!==false) {
			$this->success("删除成功！",U("Cashimadmin/kucun"));
		} else {
			$this->error("删除失败！");
		}
		}
		
	}
	function showinfo(){
		$id = intval(I('get.id'));
		$cashio_model = D('Cashio');
		$showinfo = $cashio_model->where(array("id"=>$id))->find();
		$this -> assign('showinfo',$showinfo);			
		$this->display();
		//dump($showinfo['id']);
		
	}
	
		
}